package jinyilw.tools.audio;
import javax.sound.sampled.AudioFileFormat.Type;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;

public class AudioTools
{
	private AudioTools()
	{
	}

	public static void playFlac(String path) throws UnsupportedAudioFileException, IOException
	{
		if (path != null && path.toLowerCase().endsWith(".flac"))
		{
			File file = new File(path);
			if (!file.exists())
			{
				System.err.println(path + "文件不存在");
			} else
			{
				AudioInputStream stream = AudioSystem.getAudioInputStream(file);
				AudioFormat baseFormat = stream.getFormat();
				if (baseFormat.getEncoding() != Encoding.PCM_SIGNED)
				{
					baseFormat = new AudioFormat(Encoding.PCM_SIGNED, baseFormat.getSampleRate(),
							16, baseFormat.getChannels(), baseFormat.getChannels() * 2,
							baseFormat.getSampleRate(), false);
					stream = AudioSystem.getAudioInputStream(baseFormat, stream);
				}

				DataLine.Info info = new DataLine.Info(SourceDataLine.class, baseFormat, -1);
				SourceDataLine data;

				try
				{
					data = (SourceDataLine) AudioSystem.getLine(info);
					data.open(baseFormat);
					data.start();
					byte[] bt = new byte[1024];

					while (stream.read(bt) != -1)
					{
						data.write(bt, 0, bt.length);
					}

					data.drain();
					data.stop();
					data.close();
					stream.close();
				} catch (Exception var7)
				{
					throw new RuntimeException(var7.getMessage());
				}
			}
		} else
		{
			System.err.println(path + "路径错误");
		}
	}

	public static void playWav(String path) throws UnsupportedAudioFileException, IOException
	{
		if (path != null && path.toLowerCase().endsWith(".wav"))
		{
			File file = new File(path);
			if (!file.exists())
			{
				System.err.println(path + "文件不存在");
			} else
			{
				AudioInputStream stream = AudioSystem.getAudioInputStream(file);
				play(stream);
			}
		} else
		{
			System.err.println(path + "路径错误");
		}
	}

	public static void playPcm(String path) throws UnsupportedAudioFileException, IOException
	{
		if (path != null && path.toLowerCase().endsWith(".pcm"))
		{
			File file = new File(path);
			if (!file.exists())
			{
				System.err.println(path + "文件不存在");
			} else
			{
				AudioInputStream stream = AudioSystem.getAudioInputStream(file);
				play(stream);
			}
		} else
		{
			System.err.println(path + "路径错误");
		}
	}

	public static void playMp3(String path) throws UnsupportedAudioFileException, IOException
	{
		if (path != null && path.toLowerCase().endsWith(".mp3"))
		{
			File file = new File(path);
			if (!file.exists())
			{
				System.err.println(path + "文件不存在");
			} else
			{
				AudioInputStream stream = AudioSystem.getAudioInputStream(file);
				AudioFormat inFormat = stream.getFormat();
				AudioFormat format = getOutFormat(inFormat);
				stream = AudioSystem.getAudioInputStream(format, stream);
				play(stream);
			}
		} else
		{
			System.err.println(path + "路径错误");
		}
	}

	private static void play(AudioInputStream stream)
	{
		AudioFormat target = stream.getFormat();
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, target, -1);
		SourceDataLine line;
		try (stream)
		{
			line = (SourceDataLine) AudioSystem.getLine(info);
			line.open(target);
			line.start();
			byte[] buffer = new byte[1024];

			int len;
			while ((len = stream.read(buffer)) > 0)
			{
				line.write(buffer, 0, len);
			}

			line.drain();
			line.stop();
			line.close();
		} catch (Exception var9)
		{
			throw new RuntimeException(var9.getMessage());
		}
	}

	private static AudioFormat getOutFormat(AudioFormat inFormat)
	{
		int ch = inFormat.getChannels();
		float rate = inFormat.getSampleRate();
		return new AudioFormat(Encoding.PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
	}

	public static AudioInputStream readPcm(String path)
			throws UnsupportedAudioFileException, IOException
	{
		File file = new File(path);
		if (!file.exists())
		{
			return null;
		} else
		{
			AudioInputStream stream = AudioSystem.getAudioInputStream(file);
			AudioFormat format = stream.getFormat();
			System.out.println(format.toString());
			return stream;
		}
	}

	public static void wavToPcm(String wpath, String ppath)
	{
		File file = new File(wpath);
		if (!file.exists())
		{
			throw new RuntimeException("文件不存在:" + wpath);
		} else
		{
			try
			{
				AudioInputStream stream1 = AudioSystem.getAudioInputStream(file);
				AudioInputStream stream2 = AudioSystem.getAudioInputStream(Encoding.PCM_SIGNED,
						stream1);
				AudioSystem.write(stream2, Type.WAVE, new File(ppath));
				stream2.close();
				stream1.close();
			} catch (IOException | UnsupportedAudioFileException var5)
			{
				throw new RuntimeException(var5.getMessage());
			}
		}
	}
}